package com.xros.demo.gamedataserver.decoder;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;

import java.util.List;

/**
 * @Author: mzp
 * @Description: 自定义的帧解码器
 * @Date: 2024/3/26 16:51
 * @Version: 1.0
 */
public class CustomFrameDecoder extends LengthFieldBasedFrameDecoder {

    //region （描述：静态属性）

    static int DEFAULT_MAX_FRAME_LENGTH = Integer.MAX_VALUE;

    static int DEFAULT_LENGTH_FIELD_OFFSET = 11;


    static int DEFAULT_LENGTH_FIELD_LENGTH = 4;

    static int DEFAULT_LENGTH_ADJUSTMENT = 1;

    static int DEFAULT_INITIAL_BYTES_TO_STREAM = 0;


    //endregion

    /**
     * 单次传输接收的最大字节长度
     */
    int maxFrameLength;
    /**
     * 长度字段的偏移量
     */
    int lengthFieldOffset;
    /**
     * 长度字段的长度
     */
    int lengthFieldLength;

    /**
     * 长度字段为基准，还有几个字节是内容
     */
    int lengthAdjustment;

    /**
     * 从头剥离几个字节
     */
    int initialBytesToStrip;


    public CustomFrameDecoder() {
        super(DEFAULT_MAX_FRAME_LENGTH, DEFAULT_LENGTH_FIELD_OFFSET, DEFAULT_LENGTH_FIELD_LENGTH, DEFAULT_LENGTH_ADJUSTMENT, DEFAULT_INITIAL_BYTES_TO_STREAM);


        //自定义协议格式：
        //  魔术头(4字节) ，类型：int
        //  版本号(1字节) ，类型：byte
        //  序列化算法（1字节），类型：byte
        //  消息类型(1字节) ，类型：byte
        //  序列号(4字节)，类型：int
        //  数据长度(4字节)，类型：int
        //  caffe（1字节），类型、；int
        //  数据内容 ？？？

        this.maxFrameLength = DEFAULT_MAX_FRAME_LENGTH;
        this.lengthFieldOffset = DEFAULT_LENGTH_FIELD_OFFSET;
        this.lengthFieldLength = DEFAULT_LENGTH_FIELD_LENGTH;
        this.lengthAdjustment = DEFAULT_LENGTH_ADJUSTMENT;
        this.initialBytesToStrip = DEFAULT_INITIAL_BYTES_TO_STREAM;
    }

    public CustomFrameDecoder(int maxFrameLength, int lengthFieldOffset, int lengthFieldLength, int lengthAdjustment, int initialBytesToStrip) {

        super(maxFrameLength, lengthFieldOffset, lengthFieldLength, lengthAdjustment, initialBytesToStrip);
        this.maxFrameLength = maxFrameLength;
        this.lengthFieldOffset = lengthFieldOffset;
        this.lengthFieldLength = lengthFieldLength;
        this.lengthAdjustment = lengthAdjustment;
        this.initialBytesToStrip = initialBytesToStrip;
    }

//    @Override
//    protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
//
//        Object decode = super.decode(ctx, in);
//        return decode;
//    }
}
